package priv.conceit.study.book.lambda.chapter3;


import lombok.AllArgsConstructor;
import lombok.Data;
import priv.conceit.study.book.lambda.common.entity.Album;
import priv.conceit.study.book.lambda.common.entity.Artist;
import priv.conceit.study.book.lambda.common.entity.Track;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toSet;

/**
 * 重构遗留代码
 * 3.4
 *
 * @author Conceit
 * @since 1.0.0, 2021/9/12
 */
@Data
@AllArgsConstructor
public class RefactoringLegacyCode {


    /**
     * 寻找专辑列表中，歌曲长度大于100的歌曲名
     * @param args
     */
    public static void main(String[] args) {

        List<Album> albums=new ArrayList<>();
        Album album1=new Album("专辑1",
                Arrays.asList(new Track("专辑1-歌曲1",100),new Track("专辑1-歌曲2",200)),
                Arrays.asList(new Artist("zhaohang","china"),new Artist("zhaojie","china")));
        Album album2=new Album("专辑2",
                Arrays.asList(new Track("专辑2-歌曲1",300),new Track("专辑2-歌曲2",500)),
                Arrays.asList(new Artist("zhaohang","china"),new Artist("zhaojie","china")));
        albums.add(album1);
        albums.add(album2);
        Set<String> names=doExec(albums);
        names.stream().forEach(System.out::println);

    }

    public static Set<String> doExec(List<Album> albums){
        return  albums
                .stream()
                .flatMap(album -> album.getTracks())
                .filter(track -> track.getLength()>200)
                .map(Track::getName)
                .collect(toSet());
    }
}
